{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **LangChain Search**"
      ],
      "metadata": {
        "id": "ExQu-G_--Pd9"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "Install the necessary packages"
      ],
      "metadata": {
        "id": "iJiK00mp-aT_"
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "OuxPlA9r-Mmf"
      },
      "outputs": [],
      "source": [
        "!pip install streamlit\n",
        "!pip install langchain\n",
        "!pip install openai\n",
        "!pip install google-search-results"
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "Create a Streamlit app and save to a local file"
      ],
      "metadata": {
        "id": "AWnIeyS8-pxn"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "%%writefile app.py\n",
        "import streamlit as st\n",
        "from langchain.llms.openai import OpenAI\n",
        "from langchain.agents import load_tools, initialize_agent\n",
        "\n",
        "# Streamlit app\n",
        "st.title('LangChain Search')\n",
        "\n",
        "# Get OpenAI API key, SERP API key and search query\n",
        "openai_api_key = st.text_input(\"OpenAI API Key\", type=\"password\")\n",
        "serpapi_api_key = st.text_input(\"SERP API Key\", type=\"password\")\n",
        "search_query = st.text_input(\"Search Query\")\n",
        "\n",
        "# Check if the 'Search' button is clicked\n",
        "if st.button(\"Search\"):\n",
        "    # Validate inputs\n",
        "    if not openai_api_key.strip() or not serpapi_api_key.strip() or not search_query.strip():\n",
        "        st.write(f\"Please provide the missing fields.\")\n",
        "    else:\n",
        "        try:\n",
        "            # Initialize the OpenAI module, load the SerpApi tool, and run the search query using an agent\n",
        "            llm=OpenAI(temperature=0, openai_api_key=openai_api_key, verbose=True)\n",
        "            tools = load_tools([\"serpapi\"], llm, serpapi_api_key=serpapi_api_key)\n",
        "            agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)\n",
        "\n",
        "            result = agent.run(search_query)\n",
        "            \n",
        "            st.write(result)\n",
        "        except Exception as e:\n",
        "            st.write(f\"An error occurred: {e}\")"
      ],
      "metadata": {
        "id": "HuajRoiw-tNX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Install localtunnel to serve the Streamlit app"
      ],
      "metadata": {
        "id": "xWvZXI1y-xNo"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!npm install localtunnel"
      ],
      "metadata": {
        "id": "lBKjlQgJ-zcD"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Run the Streamlit app in the background"
      ],
      "metadata": {
        "id": "xR_VC1gm-1LR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!streamlit run app.py &>/content/logs.txt &"
      ],
      "metadata": {
        "id": "qVJ_KsOE-2-o"
      },
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "Expose the Streamlit app on port 8501"
      ],
      "metadata": {
        "id": "Cqvg_8Xm-5BG"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!npx localtunnel --port 8501"
      ],
      "metadata": {
        "id": "QEUtr2Sy-66v"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}